package org.xlp.auto.create;

import org.xlp.auto.create.exception.AutoCreatorDataLoseException;
import org.xlp.utils.XLPStringUtil;

/**
 * Description: Controller java文件自动创建类
 * <br/>date: 2023/12/04 11:05
 *
 * @version 1.0
 * @author: xlp
 */
public class ControllerAutoCreator extends AbstractAutoCreator{
    private ServiceAutoCreator serviceAutoCreator;

    /**
     * 存储对应接口名称
     */
    private String serviceName;

    public ControllerAutoCreator(){
        setSubPackageName(Constants.CONTROLLER_SUB_PACKAGE_NAME);
    }

    /**
     * 构造函数
     * @param entityName
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    public ControllerAutoCreator(String entityName) {
        this();
        setEntityName(entityName);
        _setSubPkgName(entityName);
    }

    /**
     * 构造函数
     * @param serviceAutoCreator
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    public ControllerAutoCreator(ServiceAutoCreator serviceAutoCreator) {
        super(serviceAutoCreator);
        this.serviceAutoCreator = serviceAutoCreator;
        this.serviceName = getServiceName();
        _setSubPkgName(getEntityName());
    }

    /**
     * 构造函数
     *
     * @param abstractAutoCreator
     * @throws NullPointerException 假如参数为null，则抛出该异常
     */
    public ControllerAutoCreator(AbstractAutoCreator abstractAutoCreator) {
        super(abstractAutoCreator);
        _setSubPkgName(getEntityName());
    }

    private void  _setSubPkgName(String entityName){
        String subPkgName = Constants.CONTROLLER_SUB_PACKAGE_NAME + "." + entityName.toLowerCase();
        setSubPackageName(subPkgName);
    }


    private String getServiceName(){
        if (serviceAutoCreator != null) {
            try {
                return serviceAutoCreator.getFileName().replace(Constants.JAVA_FILE_SUFFIX, "");
            } catch (AutoCreatorDataLoseException ignored) {
            }
        }
        return null;
    }

    /**
     * Java文件导入包内容
     *
     * @return
     */
    @Override
    protected String makeImportContent() {
        String importContent = "import org.xlp.mvc.annotation.CBController;\n" +
                               "import org.xlp.mvc.annotation.RequestMapping;\n";

        String packageName = StringUtils.getFullPackageName(serviceAutoCreator);
        if (!XLPStringUtil.isEmpty(packageName)){
            importContent += "import org.xlp.bean.annotation.AutoFill;\n";
            importContent += "import " + packageName + "." + serviceName + ";\n";
        }
        // 导入包
        return importContent;
    }

    /**
     * java文件注解类容
     *
     * @return
     */
    @Override
    protected String makeAnnotationContent() {
        return "@CBController(exceptionHandlerClass = )\n" +
               "@RequestMapping(\"\")\n";
    }

    /**
     * 获取java文件名称
     *
     * @return
     * @throws AutoCreatorDataLoseException 假如获取失败，则抛出该异常
     */
    @Override
    protected String getFileName() throws AutoCreatorDataLoseException {
        if (XLPStringUtil.isEmpty(getEntityName())){
            throw new AutoCreatorDataLoseException("实体名称未设置。");
        }
        return getEntityName() + "Controller" + Constants.JAVA_FILE_SUFFIX;
    }

    /**
     * java文件对应的类主体内容
     *
     * @return
     */
    @Override
    protected String makeBodyContent() {
        StringBuilder sb = new StringBuilder();
        if (serviceAutoCreator != null) {
            String serviceVar = StringUtils.firstCharToLowerCase(serviceName);
            sb.append("\t@AutoFill\n");
            sb.append("\tprivate ").append(serviceName).append(" ").append(serviceVar).append(";\n");
            sb.append("\n");
            sb.append("\tpublic void set").append(serviceName).append("(")
                    .append(serviceName).append(" ").append(serviceVar).append(") {\n");
            sb.append("\t\tthis.").append(serviceVar).append(" = ").append(serviceVar).append(";\n");
            sb.append("\t}\n");
        }
        return sb.length() == 0 ? super.makeBodyContent() : sb.toString();
    }
}
